const MusicHttp = require("../../utils/MusicHttp")
Page({
  data: {
    banners: [],
    autoplay: true,
    interval: 3000,
    duration: 1200,
    playlists: [],
    city:[]
  },
  onLoad: async function () {
    wx.getLocation({
      altitude: 'altitude',
      highAccuracyExpireTime: 0,
      isHighAccuracy: true,
      type: 'type',
      success: (res) => {
        var { latitude, longitude } = res
        var key = 'DQ6BZ-FKGW3-EWI3L-YCKAB-KBQZ3-S6B2W'
        var koa = `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${key}&get_poi=1`
        wx.request({
          url: koa,
          data: {},
          method: 'GET',
          success: (res) => {
            var currentCity = res.data.result.address_component.city
            var Storage = wx.getStorageSync('city')
            if (currentCity != Storage) {
              wx.showModal({
                title: '切换城市',
                content: '检测到当前城市与您所在城市不符合',
                showCancel: true,
                cancelText: '取消',
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F',
                success: (result) => {
                  if (result.confirm) {
                    this.setData({
                      city: currentCity
                    })
                    wx.setStorageSync('city', currentCity)
                  }
                },
              });
            }
          }
        })
      }
    })
    var res = await MusicHttp.getBanner()
    var banners = res.data.banners
    console.log(banners)
    this.setData({
      banners
    })
    var res = await MusicHttp.getTop()
    var top = res.data.playlists
    var playlists = top.splice(0, 3)
    console.log(playlists)
    this.setData({
      playlists
    })
    var res = await MusicHttp.getAblum()
    var albums = res.data.albums
    var newest = []
    albums.splice(0, 3).forEach(item => {
      var { blurPicUrl, name } = item
      newest.push({
        name,
        coverImgUrl: blurPicUrl
      })

    })
    this.setData({
      newest
    })
    var res = await MusicHttp.getDj()
    var result = res.data.result
    var dj = []
    result.splice(0, 3).forEach(item => {
      var { picUrl, name } = item
      dj.push({
        name,
        coverImgUrl: picUrl
      })
    })
    this.setData({
      dj
    })
  },
  cityClick() {
    wx.navigateTo({
      url: '/pages/city/city',
    })
  },
  onShow() {
    var city = wx.getStorageSync('city')
    this.setData({
      city
    })
  },
  handleClick(url) {
    var { url } = url.currentTarget.dataset;
    console.log(url)
    wx.navigateTo({
      url: `/pages/detail/detail?url=${url}`
    })
  },
  handleInput() {
    wx.navigateTo({
      url:`/pages/search/search`
    })
  }
  
})